<?php
/**
 * 扩展
 */

class Extend_DatabasesController extends Cms_AdminController {
	/**
	 * 初始化
	 */
	public function init() {
		parent::init ();
	}
	
	/**
	 * 数据库管理
	 */
	public function indexAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
		
		} else {
			
			$this->view->setScriptFile ( 'admin/admin_common.js' );
			
			$config = Zend_Registry::get ( 'config' );
			$dbname = $config->databases->app->config->dbname;
			
			$Model_Databases = new Model_Databases ();
			$tables = $Model_Databases->listTables ();
			
			$this->view->assign ( 'dbname', $dbname );
			$this->view->assign ( 'tables', $tables );
			
			$this->render ( 'Extend/databases/index', null, true );
		}
	}
	
	/**
	 * 数据库导出
	 */
	public function exportAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$sizelimit = $this->_request->getPost ( 'sizelimit' );
			$sqlcompat = $this->_request->getPost ( 'sqlcompat' );
			$sqlcharset = $this->_request->getPost ( 'sqlcharset' );
			$pdo_select = $this->_request->getPost ( 'pdo_select' );
			
			if (empty ( $sizelimit )) {
				$sizelimit = 2048;
			}
			
			$sizelimit = $sizelimit * 1024;
			
			$bkfile = APP_DATABASES_PATH . "zendcms_" . substr ( md5 ( time () . mt_rand ( 1000, 5000 ), 0, 16 ) ) . ".txt";
			
			dump ( $_POST );
			exit ();
		} else {
			$this->view->setScriptFile ( 'admin/admin_common.js' );
			
			$this->render ( 'Extend/databases/export', null, true );
		}
	}
	
	/**
	 * 数据库导入
	 */
	public function importAction() {
		
		if (isset ( $dosubmit )) {
			dump ( $_POST );
			exit ();
		} else {
			$this->view->setScriptFile ( 'admin/admin_common.js' );
			$this->render ( 'Extend/databases/import', null, true );
		}
	}
	
	/**
	 * 数据库优化
	 */
	public function operationAction() {
		$operation = trim ( $this->_request->get ( 'operation' ) );
		$tables = trim ( $this->_request->get ( 'tables' ) );
		
		if ($operation == 'optimize') {
			$Model_Databases = new Model_Databases ();
			$Model_Databases->Operation ( $tables );
			$this->view->perRedirect ( '优化成功！', "/extend/databases/" );
		} else {
			$Model_Databases = new Model_Databases ();
			$Model_Databases->Repair ( $tables );
			$this->view->perRedirect ( '修复成功！', "/extend/databases/" );
		}
	}
	
	/**
	 * 批量操作
	 */
	public function batchAction() {
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		$tables = $this->_request->getPost ( 'tables' );
		
		if (count ( $tables ) <= 0) {
			$this->view->perRedirect ( '请至少选择一个！', "/extend/databases/" );
		}
		
		if ($dosubmit == 'optimize') {
			$Model_Databases = new Model_Databases ();
			if (is_array ( $tables )) {
				foreach ( $tables as $table_name ) {
					$Model_Databases->Operation ( $table_name );
				}
			}
			$this->view->perRedirect ( '优化成功！', "/extend/databases/" );
		} else {
			$Model_Databases = new Model_Databases ();
			foreach ( $tables as $table_name ) {
				$Model_Databases->Repair ( $table_name );
			}
			$this->view->perRedirect ( '修复成功！', "/extend/databases/" );
		}
	}
	
	/**
	 * 显示表结构
	 */
	public function showtableAction() {
		$tables = trim ( $this->_request->get ( 'tables' ) );
		$Model_Databases = new Model_Databases ();
		$data = $Model_Databases->showTable ( $tables );
		$this->view->assign ( 'data', $data );
		$this->render ( 'Extend/databases/showtable', null, true );
	}
}
?>